// utils/api.js

const BASE_URL = 'https://your-smart-home-api.com'

// 设备API
const deviceAPI = {
  // 获取设备列表
  getDeviceList() {
    return new Promise((resolve, reject) => {
      // 模拟API调用
      setTimeout(() => {
        const mockDevices = [
          {
            id: 'light-001',
            name: '客厅主灯',
            type: 'light',
            room: 'livingroom',
            roomName: '客厅',
            icon: '/images/devices/light.png',
            status: 'online',
            powerState: 'off',
            brightness: 80,
            supportColor: true,
            color: 'warm',
            isFavorite: true
          },
          {
            id: 'ac-001',
            name: '客厅空调',
            type: 'ac',
            room: 'livingroom',
            roomName: '客厅',
            icon: '/images/devices/ac.png',
            status: 'online',
            powerState: 'off',
            temperature: 24,
            mode: 'cool',
            isFavorite: true
          },
          {
            id: 'tv-001',
            name: '客厅电视',
            type: 'tv',
            room: 'livingroom',
            roomName: '客厅',
            icon: '/images/devices/tv.png',
            status: 'online',
            powerState: 'off',
            isFavorite: false
          },
          {
            id: 'light-002',
            name: '卧室灯',
            type: 'light',
            room: 'bedroom',
            roomName: '卧室',
            icon: '/images/devices/light.png',
            status: 'online',
            powerState: 'off',
            brightness: 50,
            supportColor: false,
            isFavorite: true
          }
        ]
        resolve(mockDevices)
      }, 500)
    })
  },
  
  // 控制设备
  controlDevice(deviceId, command) {
    return new Promise((resolve, reject) => {
      // 模拟API调用
      setTimeout(() => {
        console.log(`控制设备 ${deviceId}:`, command)
        resolve({ success: true, message: '控制成功' })
      }, 300)
    })
  },
  
  // 获取设备状态
  getDeviceStatus(deviceId) {
    return new Promise((resolve, reject) => {
      // 模拟API调用
      setTimeout(() => {
        const status = {
          powerState: Math.random() > 0.5 ? 'on' : 'off',
          online: Math.random() > 0.2
        }
        resolve(status)
      }, 200)
    })
  }
}

// 场景API
const sceneAPI = {
  // 获取场景列表
  getSceneList() {
    return new Promise((resolve, reject) => {
      // 模拟API调用
      setTimeout(() => {
        const mockScenes = [
          {
            id: 'home',
            name: '回家模式',
            icon: '/images/scenes/home.png',
            description: '打开客厅灯光，调节舒适温度',
            devices: ['light-001', 'ac-001']
          },
          {
            id: 'away',
            name: '离家模式',
            icon: '/images/scenes/away.png',
            description: '关闭所有设备，开启安防',
            devices: ['light-001', 'light-002', 'ac-001', 'tv-001']
          }
        ]
        resolve(mockScenes)
      }, 500)
    })
  },
  
  // 激活场景
  activateScene(sceneId) {
    return new Promise((resolve, reject) => {
      // 模拟API调用
      setTimeout(() => {
        console.log(`激活场景: ${sceneId}`)
        resolve({ success: true, message: '场景已激活' })
      }, 1000)
    })
  }
}

// 房间API
const roomAPI = {
  // 获取房间列表
  getRoomList() {
    return new Promise((resolve, reject) => {
      // 模拟API调用
      setTimeout(() => {
        const mockRooms = [
          {
            id: 'livingroom',
            name: '客厅',
            icon: '/images/rooms/livingroom.png',
            deviceCount: 3,
            onlineCount: 3
          },
          {
            id: 'bedroom',
            name: '卧室',
            icon: '/images/rooms/bedroom.png',
            deviceCount: 2,
            onlineCount: 2
          },
          {
            id: 'kitchen',
            name: '厨房',
            icon: '/images/rooms/kitchen.png',
            deviceCount: 1,
            onlineCount: 1
          }
        ]
        resolve(mockRooms)
      }, 500)
    })
  }
}

module.exports = {
  deviceAPI,
  sceneAPI,
  roomAPI
}